<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BScreenJoinSpace
 *
 * @author jianfeng
 */
class BScreenJoinSpace extends BUIScreen {

    //put your code here

    private $senderPermits = array();
    private $receiverPermits = array();
    public $joinSpaceModel;

    public function onRender($config = null, $style = NULL) {
        //parent::render();

        $activeSpaceUI = Blyn::app()->getActiveSpace()->getUIManager();

        //remove SpaceTopBar 
        if (isset($activeSpaceUI->views['SpaceTopBar']))
            unset($activeSpaceUI->views['SpaceTopBar']);

        $activeAppUI = Blyn::app()->getActiveApp()->getUIManager();

        //remove AppTopBar
        if (isset($activeAppUI->views['AppTopBar']))
            unset($activeAppUI->views['AppTopBar']);

        $this->joinSpaceModel = new JoinSpaceFormModel();
        if (isset($_POST['JoinSpaceFormModel'])) {
            $this->joinSpaceModel->attributes = $_POST['JoinSpaceFormModel'];
            //$screen->renderView('leftMenu');
        }

        $this->addRenderView('joinSpace', 'joinSpace', array('model' => $this->joinSpaceModel, 'screen' => $this));
    }

    public function findSpaces($searchText) {

        $qualifiedSpaces = array();

        $spaces = BSpace::findAll();

        foreach ($spaces as $space) {

            if ($space instanceof BSpace) {
                $allowJoinRoles = $space->getSpaceEngine()->getAllowJoinSpaceRoles();
                if ($allowJoinRoles != NULL)
                    $qualifiedSpaces[$space->getId()] = $space;
                /*
                  $user = Blyn::app()->getCurrentUser();
                  if ($user->isMemberOfRoles($allowJoinRoles, TRUE))
                  $qualifiedSpaces[$space->getId()] = $space;
                 * 
                 */
            }
        }

        $userSpaces = Blyn::app()->getCurrentUser()->getUserSpaces();

        foreach ($userSpaces as $spaces) {
            foreach ($spaces as $id => $space) {
                unset($qualifiedSpaces[$id]);
            }
        }

        return $qualifiedSpaces;
    }

    public function sendJoinSpaceRequest($spaceId, $senderMessage, $choosedRole) {

        $receiverPermits = array();
        $joinSpaceContract = new BOCJoinSpace(array('spaceId' => $spaceId));

        if ($this->receiverPermits != NULL) {
            $receiverPermits = $this->receiverPermits[$spaceId];
        } else {
            $opContractData = $this->getOperationContract();
            $opContract = $opContractData['operationContract'];
            $receiverPermits = $opContract->getOperationPermits('joinSpace', 'receiverPermit');
        }

        foreach ($receiverPermits as $role) {
            $result = $joinSpaceContract->sendOperationRequest(
                    array('name' => 'joinSpace',
                'data' => array('senderMessage' => $senderMessage, 'applyingRole' => $choosedRole)), array('role' => $role, 'spaceId' => $spaceId));
        }
    }

    public function getSenderPermits($spaceId = NULL) {

        if ($this->senderPermits == NULL) {
            $opContractData = $this->getOperationContract();
            $opContract = $opContractData['opContract'];
            $myRole = $opContractData['myRole'];
            return $opContract->getOperationPermits('joinSpace', 'senderPermit');
        }
        if ($spaceId == NULL)
            return $this->senderPermits;
        else {
            return $this->senderPermits[$spaceId];
        }
    }

    private function getOperationContract() {
        $activeApp = Blyn::app()->getActiveApp();
        $operationConfig = $activeApp->getConfigManager()->getConfigManager('appOperations');
        $joinSpaceConfig = $operationConfig['joinSpace'];

        if (isset($joinSpaceConfig['operationContract'])) {
            $class = $joinSpaceConfig['operationContract']['class'];
            $opContract = new $class;
            $myRole = $joinSpaceConfig['operationContract']['myRole'];
        }

        return array('operationContract' => $opContract, 'myRole' => $myRole);
    }

}

?>
